package threadedSimulator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import Devices.IODevices;
import Devices.KeyboardIODevice;
import Devices.MouseIODevice;
import Processes.KeyboardProcess;
import Processes.Processes;
import Schedulers.PriorityScheduler;
import Schedulers.RoundRobinScheduler;
import Schedulers.Scheduler;
import Schedulers.lotteryScheduler;

/**
 * 
 * @author Kris
 * @version 03/13/2014
 */
public class Threading2Gui extends JFrame {

	private JList my_panel;
	private JScrollPane my_scroll_pane;
	private KeyListener my_key_listener;
	private MouseListener my_mouse_adapter;
	private Cpu my_cpu;
	private IODevices my_IO_keyboard;
	private IODevices my_IO_mouse;
	private Processes my_processes;
	private Scheduler my_scheduler;
	private JDialog my_scheduler_chooser;
	private JButton my_pause_button;
	private List<JTextField> my_text_fields = new LinkedList<JTextField>(
			Arrays.asList(
					new JTextField("Enter Number of Keyboard Processes."),
					new JTextField("Enter Number of Producer/Consumer Processes.")));
	
	private List<JButton> my_buttons = new LinkedList<JButton>(Arrays.asList(
			new JButton("Lottery"), new JButton("Priority"), new JButton(
					"Round Robin")));
	

	/**
	 * 
	 */
	Threading2Gui() {
		my_scheduler_chooser = new JDialog();
		my_scheduler_chooser.setLayout(new FlowLayout());
		my_panel = new JList();
		my_scheduler_chooser.addWindowListener(new WindowListener() {
			@Override
			public void windowActivated(WindowEvent arg0) {}
			@Override
			public void windowClosed(WindowEvent arg0) {}
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}
			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}
			@Override
			public void windowIconified(WindowEvent arg0) {
			}
			@Override
			public void windowOpened(WindowEvent arg0) {
			}
		});

		int numberof_compute = 0;
		int numberof_consumer = 0;
		for (final JTextField text : my_text_fields) {
			text.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					text.setText("");
				}
			});
			my_scheduler_chooser.add(text);
		}

		my_scheduler_chooser.setTitle("Choose which Scheduler you would like to use.");

		for (final JButton button : my_buttons) {
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					boolean flag = false;
					for (final JTextField text : my_text_fields) {
						if (text.getText().equals("")) {
							flag = true;
						}
					}
                    
					my_cpu = new Cpu(my_panel, my_text_fields.get(0).getText(), my_text_fields.get(0).getText());
					if (!flag) {
						String name = button.getText();
						switch (name) {
						case "Lottery":
							my_scheduler = new lotteryScheduler(my_cpu);
							break;
						case "Priority":
							my_scheduler = new PriorityScheduler(my_cpu);
							break;
						case "Round Robin":
							my_scheduler = new RoundRobinScheduler(my_cpu);
							break;
						default:
							break;
						}
						my_cpu.addScheduler(my_scheduler);
						// my_cpu.my_scheduler = my_scheduler;
						button.setVisible(false);
						my_IO_keyboard = new KeyboardIODevice(my_cpu, my_text_fields.get(0).getText());
						my_IO_mouse = new MouseIODevice(my_cpu, my_text_fields.get(0).getText());
						start();
					} else {
						JOptionPane.showMessageDialog(null, "Invalid Input",
								"Alert", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			my_scheduler_chooser.add(button);
		}

		my_scheduler_chooser.setSize(340, 200);
		my_scheduler_chooser.setVisible(true);
	}

	private void start() {

		my_scroll_pane = new JScrollPane();
		my_scroll_pane.setViewportView(my_panel);
		add(my_scroll_pane);
		my_pause_button = new JButton("Pause");
		my_pause_button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				my_cpu.pause();
			}
		});
		my_pause_button.setSize(100, 30);
		my_pause_button.setVisible(true);
		my_pause_button.setFocusable(false);
//		setLayout(new BorderLayout());
		add(my_pause_button, BorderLayout.NORTH);
		my_cpu.setScroll(my_scroll_pane);
		my_panel.setVisible(true);
//		my_scroll_pane.set
//		my_panel.setBackground(Color.WHITE);
//		my_panel.setPreferredSize(new Dimension(490, 490));

//		my_scroll_pane.setValue(my_scroll_pane.getMaximumSize() );
		
		
		setResizable(false);
		setSize(500, 500);
		setVisible(true);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		my_panel.addKeyListener(my_key_listener = new KeyListener() {

			@Override
			public void keyPressed(KeyEvent arg0) {
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				my_cpu.pauseFor(5000, "Keybooard");
			}
		});

		my_panel.addMouseListener(my_mouse_adapter = new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				my_cpu.pauseFor(5000, "Mouse");
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});
//		my_panel.setFocusable(false);
	}
}
